package com.ruoyi.dynamic.controller.app;

import com.ruoyi.common.core.constant.constant.PathConstant;
import com.ruoyi.common.core.context.SecurityContextHolder;
import com.ruoyi.common.core.pojo.result.Pages;
import com.ruoyi.common.core.pojo.vo.BaseVO;
import com.ruoyi.common.core.pojo.vo.MultiVO;
import com.ruoyi.common.core.pojo.vo.PageVO;
import com.ruoyi.common.core.pojo.vo.SingleVO;
import com.ruoyi.common.core.result.Result;
import com.ruoyi.dynamic.model.pojo.entity.RoomBanUser;
import com.ruoyi.dynamic.model.pojo.entity.RoomPermission;
import com.ruoyi.dynamic.model.pojo.entity.RoomPost;
import com.ruoyi.dynamic.model.pojo.request.app.*;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomUserPostInfoResult;
import com.ruoyi.dynamic.model.pojo.result.app.AppRoomUserPostResult;
import com.ruoyi.dynamic.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


@Tag(name = "App-房间马甲设置", description = "App-房间马甲设置")
@RestController
@RequestMapping(PathConstant.APP_PATH + "room-post")
public class AppRoomUserPostController {

    private final IRoomPostService iRoomPostService;
    private final IRoomPermissionService iRoomPermissionService;
    private final IRoomUserPostService iRoomUserPostService;
    private final IRoomService iRoomService;
    private final IRoomBanUserService iRoomBanUserService;

    public AppRoomUserPostController(IRoomPostService iRoomPostService, IRoomPermissionService iRoomPermissionService, IRoomUserPostService iRoomUserPostService, IRoomService iRoomService, IRoomBanUserService iRoomBanUserService) {
        this.iRoomPostService = iRoomPostService;
        this.iRoomPermissionService = iRoomPermissionService;
        this.iRoomUserPostService = iRoomUserPostService;
        this.iRoomService = iRoomService;
        this.iRoomBanUserService = iRoomBanUserService;
    }

    @Operation(summary = "职位列表", description = "职位列表")
    @GetMapping("postList")
    public MultiVO<RoomPost> postList(@Parameter(description = "房间id") Long roomId) {
        return Result.multi(iRoomUserPostService.listByUser(roomId, SecurityContextHolder.getUserId()));
    }

    @Operation(summary = "权限列表", description = "权限列表")
    @GetMapping("perList")
    public MultiVO<RoomPermission> perList() {
        return Result.multi(iRoomPermissionService.list());
    }


    @Operation(summary = "禁言/拉黑用户列表", description = "权限列表")
    @GetMapping("banListPage")
    public PageVO<RoomBanUser> banListPage(@ParameterObject AppRoomBanUserSearchRequest request) {
        Pages<RoomBanUser> pages = iRoomBanUserService.banListPage(request, SecurityContextHolder.getUserId());
        return Result.page(pages);
    }

    @Operation(summary = "移除拉黑用户", description = "移除拉黑用户")
    @GetMapping("undock")
    public BaseVO undock(@RequestBody @Valid AppRoomAuctionStartRequest request ) {
        iRoomBanUserService.undock(request,SecurityContextHolder.getUserId());
        return Result.success();
    }

    @Operation(summary = "添加某个用户/编辑某个用户马甲", description = "添加某个用户/编辑某个用户马甲")
    @PostMapping("addOrEdit")
    public BaseVO addOrEdit(@RequestBody @Valid AppRoomPostAddOrEditRequest request) {
        boolean add = iRoomPermissionService.addOrEdit(request, SecurityContextHolder.getUserId());
        return Result.successOrFail(add);
    }

    @Operation(summary = "取消马甲", description = "取消马甲")
    @PostMapping("cancel")
    public BaseVO cancel(@RequestBody @Valid AppRoomSingDelRequest request) {
        boolean add = iRoomPermissionService.cancel(request, SecurityContextHolder.getUserId());
        return Result.successOrFail(add);
    }

    @Operation(summary = "马甲列表", description = "马甲列表")
    @GetMapping("listPage")
    public PageVO<AppRoomUserPostInfoResult> listPage(@Valid AppRoomUserPostSearchRequest request) {
        Pages<AppRoomUserPostInfoResult> appRoomUserPostInfoResultPages = iRoomUserPostService.listPage(request, SecurityContextHolder.getUserId());
        return Result.page(appRoomUserPostInfoResultPages);
    }

    @Operation(summary = "马甲详情", description = "马甲详情")
    @GetMapping("info")
    public SingleVO<AppRoomUserPostInfoResult> info(Long id) {
        return Result.single(iRoomUserPostService.info(id));
    }


    @Operation(summary = "房间禁言/拉黑某个用户", description = "房间禁言/拉黑某个用户")
    @PostMapping("banByUser")
    public BaseVO banByUser(@RequestBody @Validated AppRoomBanByUserRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        iRoomService.banByUser(request, userId);
        return Result.success();
    }

    @Operation(summary = "房间取消禁言某个用户", description = "房间取消禁言某个用户")
    @PostMapping("cancelBanUser")
    public BaseVO cancelBanUser(@RequestBody @Validated AppRoomBanByUserRequest request) {
        Long userId = SecurityContextHolder.getUserId();
        iRoomService.cancelBanUser(request, userId);
        return Result.success();
    }

    @Operation(summary = "管理员查看某个用户资料", description = "管理员查看某个用户资料")
    @PostMapping("lookUser")
    public SingleVO<AppRoomUserPostResult> lookUser(@RequestBody @Valid AppRoomBanByUserRequest request) {
        AppRoomUserPostResult result = iRoomUserPostService.lookUser(request, SecurityContextHolder.getUserId());
        return Result.single(result);
    }

}
